{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 14.11 \u8f93\u51fa\u8b66\u544a\u4fe1\u606f\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u5e0c\u671b\u81ea\u5df1\u7684\u7a0b\u5e8f\u80fd\u751f\u6210\u8b66\u544a\u4fe1\u606f\uff08\u6bd4\u5982\u5e9f\u5f03\u7279\u6027\u6216\u4f7f\u7528\u95ee\u9898\uff09\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8981\u8f93\u51fa\u4e00\u4e2a\u8b66\u544a\u6d88\u606f\uff0c\u53ef\u4f7f\u7528 warning.warn() \u51fd\u6570\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import warnings\n\ndef func(x, y, logfile=None, debug=False):\n    if logfile is not None:\n         warnings.warn('logfile argument deprecated', DeprecationWarning)\n    ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "warn() \u7684\u53c2\u6570\u662f\u4e00\u4e2a\u8b66\u544a\u6d88\u606f\u548c\u4e00\u4e2a\u8b66\u544a\u7c7b\uff0c\u8b66\u544a\u7c7b\u6709\u5982\u4e0b\u51e0\u79cd\uff1aUserWarning,  DeprecationWarning,\nSyntaxWarning, RuntimeWarning, ResourceWarning, \u6216 FutureWarning."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u8b66\u544a\u7684\u5904\u7406\u53d6\u51b3\u4e8e\u4f60\u5982\u4f55\u8fd0\u884c\u89e3\u91ca\u5668\u4ee5\u53ca\u4e00\u4e9b\u5176\u4ed6\u914d\u7f6e\u3002\n\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u4f7f\u7528 -W all \u9009\u9879\u53bb\u8fd0\u884cPython\uff0c\u4f60\u4f1a\u5f97\u5230\u5982\u4e0b\u7684\u8f93\u51fa\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "bash % python3 -W all example.py\nexample.py:5: DeprecationWarning: logfile argument is deprecated\n  warnings.warn('logfile argument is deprecated', DeprecationWarning)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u901a\u5e38\u6765\u8bb2\uff0c\u8b66\u544a\u4f1a\u8f93\u51fa\u5230\u6807\u51c6\u9519\u8bef\u4e0a\u3002\u5982\u679c\u4f60\u60f3\u8bb2\u8b66\u544a\u8f6c\u6362\u4e3a\u5f02\u5e38\uff0c\u53ef\u4ee5\u4f7f\u7528 -W error \u9009\u9879\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "bash % python3 -W error example.py\nTraceback (most recent call last):\n  File \"example.py\", line 10, in <module>\n    func(2, 3, logfile='log.txt')\n  File \"example.py\", line 5, in func\n    warnings.warn('logfile argument is deprecated', DeprecationWarning)\nDeprecationWarning: logfile argument is deprecated\nbash %"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u4f60\u7ef4\u62a4\u8f6f\u4ef6\uff0c\u63d0\u793a\u7528\u6237\u67d0\u4e9b\u4fe1\u606f\uff0c\u4f46\u662f\u53c8\u4e0d\u9700\u8981\u5c06\u5176\u4e0a\u5347\u4e3a\u5f02\u5e38\u7ea7\u522b\uff0c\u90a3\u4e48\u8f93\u51fa\u8b66\u544a\u4fe1\u606f\u5c31\u4f1a\u5f88\u6709\u7528\u4e86\u3002\n\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u51c6\u5907\u4fee\u6539\u67d0\u4e2a\u51fd\u6570\u5e93\u6216\u6846\u67b6\u7684\u529f\u80fd\uff0c\u4f60\u53ef\u4ee5\u5148\u4e3a\u4f60\u8981\u66f4\u6539\u7684\u90e8\u5206\u8f93\u51fa\u8b66\u544a\u4fe1\u606f\uff0c\u540c\u65f6\u5411\u540e\u517c\u5bb9\u4e00\u6bb5\u65f6\u95f4\u3002\n\u4f60\u8fd8\u53ef\u4ee5\u8b66\u544a\u7528\u6237\u4e00\u4e9b\u5bf9\u4ee3\u7801\u6709\u95ee\u9898\u7684\u4f7f\u7528\u65b9\u5f0f\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f5c\u4e3a\u53e6\u5916\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570\u5e93\u7684\u8b66\u544a\u4f7f\u7528\u4f8b\u5b50\uff0c\u4e0b\u9762\u6f14\u793a\u4e86\u4e00\u4e2a\u6ca1\u6709\u5173\u95ed\u6587\u4ef6\u5c31\u9500\u6bc1\u5b83\u65f6\u4ea7\u751f\u7684\u8b66\u544a\u6d88\u606f\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import warnings\nwarnings.simplefilter('always')\nf = open('/etc/passwd')\ndel f"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5e76\u4e0d\u662f\u6240\u6709\u8b66\u544a\u6d88\u606f\u90fd\u4f1a\u51fa\u73b0\u3002-W \u9009\u9879\u80fd\u63a7\u5236\u8b66\u544a\u6d88\u606f\u7684\u8f93\u51fa\u3002\n-W all \u4f1a\u8f93\u51fa\u6240\u6709\u8b66\u544a\u6d88\u606f\uff0c-W ignore \u5ffd\u7565\u6389\u6240\u6709\u8b66\u544a\uff0c-W error \u5c06\u8b66\u544a\u8f6c\u6362\u6210\u5f02\u5e38\u3002\n\u53e6\u5916\u4e00\u79cd\u9009\u62e9\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528 warnings.simplefilter() \u51fd\u6570\u63a7\u5236\u8f93\u51fa\u3002\nalways \u53c2\u6570\u4f1a\u8ba9\u6240\u6709\u8b66\u544a\u6d88\u606f\u51fa\u73b0\uff0c`ignore \u5ffd\u7565\u8c03\u6240\u6709\u7684\u8b66\u544a\uff0cerror \u5c06\u8b66\u544a\u8f6c\u6362\u6210\u5f02\u5e38\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u7b80\u5355\u7684\u751f\u6210\u8b66\u544a\u6d88\u606f\u7684\u60c5\u51b5\u8fd9\u4e9b\u5df2\u7ecf\u8db3\u591f\u4e86\u3002\nwarnings \u6a21\u5757\u5bf9\u8fc7\u6ee4\u548c\u8b66\u544a\u6d88\u606f\u5904\u7406\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u66f4\u9ad8\u7ea7\u7684\u914d\u7f6e\u9009\u9879\u3002\n\u66f4\u591a\u4fe1\u606f\u8bf7\u53c2\u8003 Python\u6587\u6863"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}